package com.learn.mvc.config;

import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.Filter;

/**
 * web工程的初始化类，代替web.xml
 */
public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer {


    /**
     * 指定当前项目的根配置类，即spring的配置类
     *
     * @return
     */
    @Override
    protected Class<?>[] getRootConfigClasses() {
        //因当前项目只是demo，不是一个完整的ssm项目，所以spring的配置不配置也行
//        return new Class[0];
        return new Class[]{SpringConfig.class};
    }

    /**
     * 指定springmvc的配置类
     *
     * @return
     */
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringmvcConfig.class};
    }

    /**
     * 指定SpringMVC的前端控制器DispatcherServlet的映射规则，即url-pattern
     * <p>
     * 即原web.xml中DispatcherServlet的：
     * <servlet-mapping>
     * <servlet-name>springMVC</servlet-name>
     * <url-pattern>/</url-pattern>
     * </servlet-mapping>
     *
     * @return
     */
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    /**
     * 添加过滤器
     *
     * @return
     */
    @Override
    protected Filter[] getServletFilters() {
        // 配置springMVC的编码过滤器
        CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter();
        encodingFilter.setEncoding("UTF-8");
        encodingFilter.setForceRequestEncoding(true);
        // 配置HiddenHttpMethodFilter过滤器  http隐藏的请求方式识别过滤器
        HiddenHttpMethodFilter hiddenHttpMethodFilter = new HiddenHttpMethodFilter();
        return new Filter[]{encodingFilter, hiddenHttpMethodFilter};
    }


}
